using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Globalization;

namespace Common.Shared {
  public static class DateTimeConverter {
    /// <summary>
    /// Converts a DateTime to Unix Epoch
    /// </summary>
    /// <param name="date">The date.</param>
    /// <returns></returns>
    public static int ToUnixEpoch ( DateTime date ) {
      TimeSpan t = ( date - new DateTime ( 1970, 1, 1 ) );
      int epoch = (int)t.TotalSeconds;
      return epoch;
    }

    /// <summary>
    /// Creates a DateTime from the seconds since Epoch
    /// </summary>
    /// <param name="seconds">The seconds.</param>
    /// <returns></returns>
    public static DateTime FromUnixEpoch ( int seconds ) {
      DateTime epoch = new DateTime ( 1970, 1, 1 );
      DateTime ret = epoch.Add ( new TimeSpan ( 0, 0, seconds ) );
      return ret;
    }

    /// <summary>
    /// Creates a DateTime from a string
    /// </summary>
    /// <param name="dateString">The date string.</param>
    /// <returns></returns>
    public static DateTime FromString ( string dateString ) {
      return DateTime.Parse ( dateString );
    }

    /// <summary>
    /// Creates a DateTime from a string
    /// </summary>
    /// <param name="dateString">The date string.</param>
    /// <param name="format">The format.</param>
    /// <returns></returns>
    public static DateTime FromString ( string dateString, IFormatProvider format ) {
      return DateTime.Parse ( dateString, format);
    }

    /// <summary>
    /// Creates a DateTime from a string
    /// </summary>
    /// <param name="dateString">The date string.</param>
    /// <param name="format">The format.</param>
    /// <param name="styles">The styles.</param>
    /// <returns></returns>
    public static DateTime FromString ( string dateString, IFormatProvider format, DateTimeStyles styles ) {
      return DateTime.Parse ( dateString, format, styles );
    }

    /// <summary>
    /// Creates a DateTime from a binary value
    /// </summary>
    /// <param name="dateData">The date data.</param>
    /// <returns></returns>
    public static DateTime FromBinary ( long dateData ) {
      return DateTime.FromBinary ( dateData );
    }

    /// <summary>
    /// Creates a DateTime from a file time
    /// </summary>
    /// <param name="fileTime">The file time.</param>
    /// <returns></returns>
    public static DateTime FromFileTime ( long fileTime ) {
      return DateTime.FromFileTime ( fileTime );
    }

    /// <summary>
    /// Creates a DateTime from a file time UTC
    /// </summary>
    /// <param name="fileTime">The file time.</param>
    /// <returns></returns>
    public static DateTime FromFileTimeUtc ( long fileTime ) {
      return DateTime.FromFileTimeUtc ( fileTime );
    }

    /// <summary>
    /// Creates a DateTime from an OA Date
    /// </summary>
    /// <param name="date">The date.</param>
    /// <returns></returns>
    public static DateTime FromOADate ( double date ) {
      return DateTime.FromOADate ( date );
    }

    /// <summary>
    /// Creates a DateTime from a string
    /// </summary>
    /// <param name="dateString">The date string.</param>
    /// <param name="dateRef">The date ref.</param>
    /// <returns></returns>
    public static bool TryParse ( string dateString, out DateTime dateRef ) {
      return DateTime.TryParse ( dateString, out dateRef );
    }

    /// <summary>
    /// Creates a DateTime from a string
    /// </summary>
    /// <param name="dateString">The date string.</param>
    /// <param name="provider">The provider.</param>
    /// <param name="styles">The styles.</param>
    /// <param name="dateRef">The date ref.</param>
    /// <returns></returns>
    public static bool TryParse ( string dateString, IFormatProvider provider, DateTimeStyles styles, out DateTime dateRef ) {
      return DateTime.TryParse ( dateString, provider, styles, out dateRef );
    }
  }
}
